//
//  DEWNoticeTableViewCell.m
//  OtherDemo
//
//  Created by lianjie on 2018/7/25.
//  Copyright © 2018年 lianjie. All rights reserved.
//

#import "DEWNoticeTableViewCell.h"
#import "MMButton.h"
@implementation DEWNoticeTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = ClearColor;
        self.contentView.backgroundColor = ClearColor;
        
    }
    return self;
}

-(void)setModel:(DEWNoticeModel *)model
{
    _model = model;
    [self createViewWithInfo:model];
}


- (void)createViewWithInfo:(DEWNoticeModel *)model
{
    NSInteger type = [model.type integerValue];
    
    CGFloat bgH = 0.0f;
    if (type == 15 || type == 21) {
        bgH = model.cellHeight-45.0f;
    }else{
        bgH = model.cellHeight;
    }
    
    ///背景
    UIView *bgView = [[UIView alloc]init];
    bgView.frame = CGRectMake(Margin_15, Margin_15, Device_Width-Margin_15*2, bgH);
    bgView.backgroundColor = WhiteColor;
    bgView.layer.cornerRadius = 4.0f;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    
    ///--------------动态视图-------------------
    CGFloat midH = 0.0f;
    CGFloat descH = [self backCommentHeigh:model.attbuteText];
    if (type == 15 || type == 21) {
        
        midH = descH+13+30;
        
    }else{
        if (descH>35.0f) {
            midH = descH;
        }else{
            midH = 35.0f;
        }
    }
    UIView *descView = [[UIView alloc]init];

    descView.frame = CGRectMake(Margin_15, 51, bgView.width-Margin_15-90, midH);
    
    [bgView addSubview:descView];
    
    ///内容
    UILabel *descLab = [[UILabel alloc]init];
    descLab.numberOfLines = 0;
    [descView addSubview:descLab];
    
    if (type == 15 || type == 21) {
        CGFloat imgWH = 20;
        UIImageView *headerImageV = [[UIImageView alloc]init];
        headerImageV.frame = CGRectMake(0, 0, imgWH, imgWH);
        [headerImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVER_URL,model.ext2]] placeholderImage:kDefalutUserHeader];
        headerImageV.layer.cornerRadius = imgWH/2;
        headerImageV.layer.masksToBounds =YES;
        [descView addSubview:headerImageV];
        
        UILabel *name = [[UILabel alloc]init];
        name.textColor = Color_66;
        name.font = [UIFont systemFontOfSize:13];
        name.text = model.ext1;
        name.frame = CGRectMake(headerImageV.right+10, 1, descView.width-headerImageV.right-10, 19);
        [descView addSubview:name];
        descLab.frame = CGRectMake(headerImageV.left, headerImageV.Wbotoom+10, descView.width, descH);
        
    }else{
        
        descLab.frame = CGRectMake(0, 0, descView.width, descH);
    }
    descLab.attributedText = model.attbuteText;
    descLab.numberOfLines = 0;
    ///---------------固定视图-------------------
    ///标题
    UILabel *titleLab = [[UILabel alloc]init];
    titleLab.frame = CGRectMake(Margin_15, 13.0f, bgView.width-70-Margin_15, 23.0f);
//    titleLab.backgroundColor = CellLineColor;
    [bgView addSubview:titleLab];
    
    NSString *strTitle = @"";
    if ([model.isRead isEqualToString:@"0"]) {
        ///未读
        strTitle = [NSString stringWithFormat:@"• %@",model.title];
    }else{
        ///已读
        strTitle = model.title;
    }
    
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:strTitle];
    
    
    if ([model.isRead isEqualToString:@"0"]) {
        [attribute setAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:UIColorFromRGB(0xFF492C)} range:NSMakeRange(0, 1)];
        [attribute setAttributes:@{NSForegroundColorAttributeName:Color_33,NSFontAttributeName:[UIFont systemFontOfSize:16.0f]} range:NSMakeRange(1, model.title.length)];
    }else{
        [attribute setAttributes:@{NSForegroundColorAttributeName:Color_33,NSFontAttributeName:[UIFont systemFontOfSize:16.0f]} range:NSMakeRange(0, model.title.length)];
    }
    
    titleLab.attributedText = attribute;
    ///查看详情
    NSString *btnTitle = @"查看详情";
    NSString *btnImg = @"LJ右箭头";
    if (type ==15 || type == 21) {
        btnTitle = @"";
        btnImg = @"";
    }else if (type == 22){
        btnTitle = @"进入群聊";
    }else if (type == 10){
        
        btnTitle = @"完善资料";
    }else if (type == 11){
        
        btnTitle = @"修改资料";
    }else if (type == 12 || type == 14){
        btnTitle = @"读取";
        btnImg = @"";
    }else if (type == 13){
        btnTitle = @"联系客服";
    }
    
    MMButton *btn = [MMButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:btnTitle forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:11];
    [btn setTitleColor:Color_99 forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:btnImg] forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(readNoticeListDetialAction) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(bgView.width-70, 17, 55, 15);
    btn.imageAlignment = MMImageAlignmentRight;
    btn.spaceBetweenTitleAndImage = 4.0f;
    [bgView addSubview:btn];
    
    
    ///时间
    UIImageView *timeImageV = [[UIImageView alloc]init];
//    timeImageV.backgroundColor = CellLineColor;
    timeImageV.left = Margin_15;
    timeImageV.Wbotoom = bgView.height-15-10;
    timeImageV.size = CGSizeMake(10, 10);
    timeImageV.image = [UIImage imageNamed:@"clock_message"];
    [bgView addSubview:timeImageV];
    ///
    UILabel *timeLab = [[UILabel alloc]init];
//    timeLab.backgroundColor = CellLineColor;
    timeLab.left = timeImageV.right+8;
    timeLab.Wbotoom = bgView.height-13.0f-15.0f;
    timeLab.size = CGSizeMake(70.0f, 15.0f);
    timeLab.textColor = UIColorFromRGB(0xAAAAAA);
    timeLab.font = [UIFont systemFontOfSize:11.0f];
    [bgView addSubview:timeLab];
    timeLab.text = [DateHelper getTimeWithTimeIntervalString:model.time];
    
    ///来源
    UILabel *sourceLab = [[UILabel alloc]init];
//    sourceLab.backgroundColor = CellLineColor;
    sourceLab.left = timeLab.right +Margin_15;
    sourceLab.Wbotoom = bgView.height-13.0f-15.0f;
    sourceLab.size = CGSizeMake(120.0f, 15.0f);
    sourceLab.textColor = UIColorFromRGB(0xAAAAAA);
    sourceLab.font = [UIFont systemFontOfSize:11.0f];
    [bgView addSubview:sourceLab];
    sourceLab.text = [NSString stringWithFormat:@"来自%@",model.fromName];
    
    ///介绍图片
    UIImageView *descImageV = [[UIImageView alloc]init];
//    descImageV.backgroundColor = CellLineColor;
    
    CGFloat descImgH = 60.0f;
    
    [descImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVER_URLIMAGE,model.picuri]] placeholderImage:defalutImage];
    descImageV.frame = CGRectMake(bgView.width-Margin_15-descImgH, titleLab.Wbotoom+Margin_15, descImgH, descImgH);
    [bgView addSubview:descImageV];
    
    
    ///////----------设置frame--------
    if (type == 15 || type == 21) {
        UIView *line = [[UIView alloc]init];
        line.x = 0;
        line.Wbotoom = 46.0f;
        line.width = bgView.width;
        line.height = 1;
        line.backgroundColor = UIColorFromRGB(0xEFEFEF);
        [bgView addSubview:line];
        
        UIView *bottomView = [[UIView alloc]init];
        bottomView.x = 0;
        bottomView.Wbotoom = 5;
        bottomView.width = bgView.height;
        bottomView.height = 40.0f;
        bottomView.backgroundColor = UIColorFromRGB(0xEFEFEF);
        [bgView addSubview:bottomView];
        
        UIButton *btn01 = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn01 setTitle:@"忽略" forState:UIControlStateNormal];
        btn01.titleLabel.font = [UIFont systemFontOfSize:15];
        btn01.tag = 101;
        [btn01 setTitleColor:Color_99 forState:UIControlStateNormal];
        [btn01 addTarget:self action:@selector(bottomViewAction:) forControlEvents:UIControlEventTouchUpInside];
        btn01.frame = CGRectMake(0, 0, bottomView.width/2-1, bottomView.height);
        [bottomView addSubview:btn01];
        
        
        UIButton *btn02 = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn02 setTitle:@"同意" forState:UIControlStateNormal];
        btn02.titleLabel.font = [UIFont systemFontOfSize:15];
        btn02.tag = 102;
        [btn02 setTitleColor:Color_99 forState:UIControlStateNormal];
        [btn02 addTarget:self action:@selector(bottomViewAction:) forControlEvents:UIControlEventTouchUpInside];
        btn02.frame = CGRectMake(0, 0, bottomView.width/2-1, bottomView.height);
        [bottomView addSubview:btn02];
        
        if ([model.isDispose isEqualToString:@"0"]) {
            ///不同意
            
            btn01.hidden = YES;
            btn02.frame = CGRectMake(0, 0, bottomView.width, 40);
            [btn02 setTitle:@"已忽略" forState:UIControlStateNormal];
            btn02.userInteractionEnabled = NO;
        }else if ([model.isDispose isEqualToString:@"1"]){
            ///同意
            btn01.hidden = YES;
            btn02.frame = CGRectMake(0, 0, bottomView.width, 40);
            btn02.userInteractionEnabled = NO;
            [btn02 setTitle:@"已同意" forState:UIControlStateNormal];
        }
        
    }
}



#pragma mark -----同意or不同意
- (void)bottomViewAction:(UIButton *)sender
{
    ///101:忽略，102:已同意;
    NSInteger tagg = sender.tag;
    BOOL isAgree = NO;
    if (tagg == 101) {
        isAgree = NO;
    }else{
        isAgree = YES;
    }
    if (_noticeDelegate && [_noticeDelegate respondsToSelector:@selector(noticeCellDidItemAtIndex:andModel:isSure:)]) {
        [_noticeDelegate noticeCellDidItemAtIndex:self.indexPath andModel:self.model isSure:isAgree];
    }
    
}

#pragma mark ---查看详情
- (void)readNoticeListDetialAction
{
    
    if (_noticeDelegate && [_noticeDelegate respondsToSelector:@selector(readNoticeCellAtItemAtIndex:andModel:)]) {
        [_noticeDelegate readNoticeCellAtItemAtIndex:self.indexPath andModel:self.model];
    }
    
}

- (CGFloat)backCommentHeigh:(NSAttributedString *)attbute
{
    CGSize attSize = [attbute boundingRectWithSize:CGSizeMake(Device_Width-Margin_15*3-90, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
    return attSize.height;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
